﻿var popupStatus = 0;
var currentPopup = null;
function loadPopup(popup) {
    //Custom Code
    currentPopup = "#" + popup;
    //loads popup only if it is disabled	
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn(100);
        $(currentPopup).fadeIn(100);
        popupStatus = 1;
    }
}

function disablePopup() {
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut(100);
        $(currentPopup).fadeOut(100);
        popupStatus = 0;
    }
}


//centering popup
function centerPopup(popup) {
    currentPopup = "#" + popup;
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(currentPopup).height();
    var popupWidth = $(currentPopup).width();
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }
    var scrollTop = yScroll + windowHeight / 2 - popupHeight / 2
    $(currentPopup).css({
        //"position": "absolute",
        //"top": windowHeight / 2 - popupHeight / 2,
        //"left": windowWidth / 2 - popupWidth / 2
        "position": "absolute",
        "top": scrollTop,
        "left": windowWidth / 2 - popupWidth / 2

    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}

window.onresize = function() {
    if (currentPopup != null)
        centerPopup(currentPopup.substring(1, currentPopup.length));
}

//jQuery Ajax Processing Function
function ShowProgress(containers) {
    var containerArray = containers.split(',');
    var processing = "<img class='processing' src='App_Themes/jQGrid/img/ajax-loader.gif' alt='Loadding.......' />";
    for (var i = 0; i < containerArray.length; i++) {
        $(containerArray[i]).html(processing);
        var marginLeft = $(containerArray[i]).width();
        var marginTop = $(containerArray[i]).height();
        var imageWidth = $('.processing').width();
        var imageHeight = $('.processing').height();
        $(containerArray[i] + ' .processing').css({ "margin-left": (marginLeft / 2) - (imageWidth / 2), "margin-top": marginTop / 2 - imageHeight / 2 });
    }
}

function ShowProgressAsync(containers, isLoaderImage, errMsg) {
    var containerArray = containers.split(',');

    var processing = "";
    var imageProcessing = '';
    if (isLoaderImage) {
        processing = "<div class='processingAsync' style='display:none'></div>";
        imageProcessing = "<div class='imageProcessingContainer'></div>";
    }
    else
        processing = "<span class='processingMsgAsync' style='display:none' >" + errMsg + "</span>";

    for (var i = 0; i < containerArray.length; i++) {
        var offset = $(containerArray[i]).offset();
        var offsetX = offset.left;
        var offsetY = offset.top;
        var divOffSetX = offsetX;
        var divOffSetY = offsetY;
        var length = $(containerArray[i]).width();
        var height = $(containerArray[i]).height();
        offsetX = offsetX + length / 2;
        offsetY = offsetY + height / 2;
        if (isLoaderImage)
            $(imageProcessing).insertAfter(containerArray[i]);
        $(processing).insertAfter(containerArray[i]);
        if (!isLoaderImage) {
            offsetX = offsetX / 2;
            $('.processing').css({ "position": "absolute", "display": "none", "left": offsetX, "top": offsetY, "z-index": "11" });
            $('.processingMsgAsync').css({ "position": "absolute", "display": "block", "width": length - offsetX, "left": offsetX, "top": offsetY, "z-index": "1" });
        }
        else {
            $('.processingAsync').css({ "position": "absolute", "display": "block", "left": offsetX, "top": offsetY, "z-index": "11" });
            $('.imageProcessingContainer').css({ "opacity": "0.5", "left": divOffSetX, "top": divOffSetY, "height": height, "width": length, "z-index": "10" });
        }
    }
}
function HideProgressAsync() {
    $('.processingAsync').fadeOut(1000, function() {
        $(this).remove();
    });
    $('.imageProcessingContainer').fadeOut(100, function() {
        $(this).remove();
    });

}
//jQuery Ajax Callback Functions
function ClientProxyDateFormatted(methodName, paramArray, successFunction, errorFunction) {
    var pagePath = window.location.pathname;
    var paramList = '';

    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '"' + ':' + '"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    var length = pagePath.length;
    if (pagePath.substring(length - 1, length) == "/") {
        pagePath += "Default.aspx";
    }
    else if (pagePath.substring(length - 5, length) != ".aspx") {
        pagePath += "/Default.aspx";
    }
    $.ajax({
        type: "POST",
        url: pagePath + "/" + methodName,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "text",
        processData: false,
        success: function(msgg) {
            var msg = JSON.parse(msgg, function(key, value) {
                var a;
                if (value != null) {
                    if (value.toString().indexOf('Date') >= 0) {
                        a = /^\/Date\((-?[0-9]+)\)\/$/.exec(value);
                        if (a) {
                            var dt = new Date(parseInt(a[1], 10));
                            return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
                        }
                    }
                    return value;
                }
            });
            successFunction(msg);
        },
        error: function(xhr, status, error) {
            errorFunction(xhr, status, error);
        }
    });
    return false;
}
function ClientProxy(methodName, paramArray, successFunction, errorFunction) {
    var pagePath = window.location.pathname;
    var paramList = '';

    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '"' + ':' + '"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    var length = pagePath.length;
    if (pagePath.substring(length - 1, length) == "/") {
        pagePath += "Default.aspx";
    }
    else if (pagePath.substring(length - 5, length) != ".aspx") {
        pagePath += "/Default.aspx";
    }

    $.ajax({
        type: "POST",
        url: pagePath + "/" + methodName,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(msg) {
            successFunction(msg);
        },
        error: function(xhr, status, error) {
            errorFunction(xhr, status, error);
        }

    });
    return false;
}



String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
String.prototype.reduceLength = function() {
    return this.substring(1, 20) + '......';
}

//String.prototype.getUnique = function(array) {
function getUnique(array) {
    return uniqueArr(array); //Print the unique value
}
function uniqueArr(a) {
    temp = new Array();
    for (i = 0; i < a.length; i++) {
        if (!contains(temp, a[i])) {
            temp.length += 1;
            temp[temp.length - 1] = a[i];
        }
    }
    return temp;
}

//Will check for the Uniqueness
function contains(a, e) {
    for (j = 0; j < a.length; j++) if (a[j] == e) return true;
    return false;
}

function GetIndex(a, e) {
    for (j = 0; j < a.length; j++) if (a[j] == e) return j;
    return null;
}

//#endregion

//Function to check that whether an element exists in an array or not
//#region
Array.prototype.ContainsValue = function(value) {
    var arrayLength = this.length;
    for (var i = 0; i < arrayLength; i++) {
        if (this[i] === value)
            return true;
    }
    return false;
}
//#endregion



//-------------------------------------------------Code for implementing paging client side--------------------------//
var currPage = 1;
var numPerPage = 5;
var numPerPageCustomer = 10;
var pageCount = 1;
//---------------------------------Global variables for Customer profile paging--------------------------------------//
var currCustomerPage = 1;
var customerPageCount = 1;

function ShowErrorMessage(message) {
    HideProgressAsync();
    var messageWrapper = '<div id="errorMessageContainer" class="popupOverlayMessage">' +
        '<span class="errorHeader"><a class="popupCloseSymbol"></a></span>' +
        '<div class="errorInformationContainer">' +
        '<span id="errorMessage"></span>' +
        '<br />' +
        '<input type="button" id="btnErrorOK" class="buttonCenter" />' +
        '</div>' +
        '<div class="backgroundPopupOverlay"></div>';
    $(messageWrapper).insertAfter('#aspnetForm');
    $('#errorMessage').html(message);
    centerPopupOverlay('errorMessageContainer');
    $("#backgroundPopupOverlay").css({
        "opacity": "0.1"
    });
    $("#backgroundPopupOverlay").fadeIn(100);
    //$(currentPopup).fadeIn(100);
    $('#errorMessageContainer').show();
    $('#errorMessageContainer')
        .bind('drag', function(event) {
            $(this).css({
                top: event.offsetY,
                left: event.offsetX
            });
        });
}
$('#btnErrorOK').live('click', function() {
    $('#errorMessageContainer').hide().remove();
});
$(".popupCloseSymbol").live('click', function() {
    disablePopup(currentPopup);
    $('#errorMessageContainer').hide().remove();
});
